package com.crm.controller.settings;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.beans.ExecutiveBusinessVO;
import com.crm.dao.ExecutivesDAO;
import com.crm.dao.OpportunityDAO;
import com.crm.dao.SupplyOrderDAO;
import com.crm.entity.Business;
import com.crm.entity.Executives;
import com.crm.entity.Opportunity;
import com.crm.util.Constants;

@Controller
@RequestMapping("/slide")
public class SlideController {
	
	@RequestMapping("/show.do")
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business business=(Business)session.getAttribute("business");
	    int busid=business.getBusinessId();
	    //OpportunityDAO oppdao=new OpportunityDAO();
	    SupplyOrderDAO sodao=new SupplyOrderDAO();
	    
	    String status=Constants.ST_PENDING;
	    List<Opportunity> pendenq=OpportunityDAO.getSingleInstance().getByStatus(status,busid);
		
	    status=Constants.ST_GENERATED_QUOTATION;
	      List<Opportunity> pendQuot=OpportunityDAO.getSingleInstance().getByStatus(status,busid);
	      
	      status=Constants.ST_GENERATED_SO;
	      List<Opportunity> pendSO=OpportunityDAO.getSingleInstance().getByStatus(status,busid);
	      status=Constants.ST_GEN_PO;
	      List<Opportunity> pendPO=OpportunityDAO.getSingleInstance().getByStatus(status,busid);
	      
	      status=Constants.ST_GENERATED_INVOICE;
	      List<Opportunity> pendInv=OpportunityDAO.getSingleInstance().getByStatus(status,busid);
	      
	      Calendar cal=Calendar.getInstance();
	      int year=cal.get(Calendar.YEAR);
	      int month=cal.get(Calendar.MONTH)+1;
	      long currEnq=OpportunityDAO.getSingleInstance().getenqs(month+"", year+"", busid).size();
	      System.out.println("Month Is:"+month);
	      
	      SupplyOrderDAO supordDAO=new SupplyOrderDAO();
	      double tot=supordDAO.getCompanyBusiness(busid);
	      double monthTurnover=supordDAO.getCompanyMonTO(year,month,busid);
	      
	     
	      
	      ExecutivesDAO exdao=new ExecutivesDAO();
	  	List<Executives> lex=exdao.findAll(busid);
	  	List<ExecutiveBusinessVO> lexbusVO=new ArrayList<ExecutiveBusinessVO>();
	  	for(int i=0;i<lex.size();i++){
	  		int id=i+1;
	  		String execName=lex.get(i).getExeName();
	  		double totBus=sodao.getExecBusiness(busid, execName);
	  		long noofOrders=sodao.getExecBusinessCount(busid, execName);
	  		ExecutiveBusinessVO exbusvo=new ExecutiveBusinessVO(id,execName,totBus,noofOrders);
	  		lexbusVO.add(exbusvo);
	  	}
	  	
	  	
	  	
		return model=new ModelAndView("/jsp/common/slideMain.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("pendEnq",pendenq.size())
		.addObject("pendQuot",pendQuot.size())
		.addObject("pendOrders",(pendSO.size()+pendPO.size()))
		.addObject("pendInv",pendInv.size())
		.addObject("execBus",lexbusVO)
		.addObject("totalBusiness",Math.round(tot))
		.addObject("currentEnqs",currEnq)
		.addObject("monthTO",Math.round(monthTurnover));
		
	}
	
}
